package com.ticket.service;

import com.ticket.dao.OrderDao;
import com.ticket.dao.TicketDao;
import com.ticket.entity.Order;
import com.ticket.entity.Ticket;

import java.util.Date;
import java.util.Map;

/**
 * @Author: lihua
 * @Date: 2024/1/13 22:09   git测试123456789
 * @Version: v1.0.0
 * @Description:
 **/
public class TicketService {

    private TicketDao ticketDao = new TicketDao();

    private OrderDao orderDao = new OrderDao();

    /**
     * 用户查询余票信息
     * @return
     */
    public String getAllTicketInfo(){
        Map<Integer,Ticket> ticketMap = ticketDao.getAllTicket();
        StringBuffer result = new StringBuffer("车票类型     起点站         终点站         剩余票数\n");
        result.append(ticketDao.mapToString(ticketMap));
        return result.toString();
    }

    /**
     * 查询用户已购车票
     * @param username
     * @return
     */
    public String getUserOrder(String username){
        Map<String,Order> orderMap = orderDao.getOrderByUsername(username);
        if(orderMap.size() > 0) {
            StringBuffer result = new StringBuffer("车票号码\t\t\t\t\t用户名\t\t\t车票类型\t\t起点站\t\t终点站\n");
            result.append(orderDao.mapToString(orderMap));
            return result.toString();
        } else
            return "该用户没有购票信息";
    }

    /**
     * 用户购票
     * @param ticketId
     * @param username
     * @return
     */
    public synchronized String buyTicket(int ticketId,String username){
        Map<Integer, Ticket> ticketMap = ticketDao.getAllTicket();
        Ticket ticket = ticketMap.get(ticketId);
        if(ticket == null){
            return "车票编号有误！";
        }else {
            if(ticket.getCount() < 1){
                return "车票已售空！";
            }else {
                //减库存
                //如果买了票1，则2，3余票也减1，如果买了2或者3，相应票数减1，1的余票与2，3较低者一至。
                //如果买了票4，则5，6余票也减1，如果买了5或者6，相应票数减1，4的余票与5，6较低者一至。
                if(ticketId == 1 || ticketId == 2 || ticketId == 3 ){
                   Ticket ticket1 = ticketMap.get(1);
                   Ticket ticket2 = ticketMap.get(2);
                   Ticket ticket3 = ticketMap.get(3);
                   if(ticketId == 2){
                       ticket2.setCount(ticket2.getCount() - 1);
                   }else if(ticketId == 3){
                       ticket3.setCount(ticket3.getCount() - 1);
                   }else {
                       ticket2.setCount(ticket2.getCount() - 1);
                       ticket3.setCount(ticket3.getCount() - 1);
                   }
                   ticket1.setCount( ticket2.getCount() > ticket3.getCount()?ticket3.getCount():ticket2.getCount() );
                   ticketMap.put(1,ticket1);
                   ticketMap.put(2,ticket2);
                   ticketMap.put(3,ticket3);
                }else {
                    Ticket ticket4 = ticketMap.get(4);
                    Ticket ticket5 = ticketMap.get(5);
                    Ticket ticket6 = ticketMap.get(6);
                    if(ticketId == 5){
                        ticket5.setCount(ticket5.getCount() - 1);
                    }else if(ticketId == 6){
                        ticket6.setCount(ticket6.getCount() - 1);
                    }else {
                        ticket5.setCount(ticket5.getCount() - 1);
                        ticket6.setCount(ticket6.getCount() - 1);
                    }
                    ticket4.setCount( ticket5.getCount() > ticket6.getCount()?ticket5.getCount():ticket6.getCount() );
                    ticketMap.put(4,ticket4);
                    ticketMap.put(5,ticket5);
                    ticketMap.put(6,ticket6);
                }
                //新增订单信息
                Order order = new Order();
                order.setOrderId(String.valueOf(new Date().getTime()));
                order.setUsername(username);
                order.setTicketId(ticketId);
                order.setStartStation(ticket.getStartStation());
                order.setEndStation(ticket.getEndStation());
                orderDao.saveOrder(order);
                ticketDao.saveAllTicket(ticketMap);
                return "购票成功";
            }
        }
    }

    /**
     * 用户退票
     * @param orderId
     * @return
     */
    public synchronized String deleteOrder(String orderId){
        Order order = orderDao.getOrderById(orderId);
        if(order == null){
            return "车票不存在！";
        }else {
            if(orderDao.deleteOrder(orderId)){
                int ticketId = order.getTicketId();
                Map<Integer,Ticket> ticketMap = ticketDao.getAllTicket();
                //如果退1，则2，3都加1，1与2，3较低者一致
                //如果退4，则5，6都加1，4与5，6较低者一致
                if(ticketId == 1 || ticketId == 2 || ticketId == 3 ){
                    Ticket ticket1 = ticketMap.get(1);
                    Ticket ticket2 = ticketMap.get(2);
                    Ticket ticket3 = ticketMap.get(3);
                    if(ticketId == 2){
                        ticket2.setCount(ticket2.getCount() + 1);
                    }else if(ticketId == 3){
                        ticket3.setCount(ticket3.getCount() + 1);
                    }else {
                        ticket2.setCount(ticket2.getCount() + 1);
                        ticket3.setCount(ticket3.getCount() + 1);
                    }
                    ticket1.setCount( ticket2.getCount() > ticket3.getCount()?ticket3.getCount():ticket2.getCount() );
                    ticketMap.put(1,ticket1);
                    ticketMap.put(2,ticket2);
                    ticketMap.put(3,ticket3);
                }else {
                    Ticket ticket4 = ticketMap.get(4);
                    Ticket ticket5 = ticketMap.get(5);
                    Ticket ticket6 = ticketMap.get(6);
                    if(ticketId == 5){
                        ticket5.setCount(ticket5.getCount() + 1);
                    }else if(ticketId == 6){
                        ticket6.setCount(ticket6.getCount() + 1);
                    }else {
                        ticket5.setCount(ticket5.getCount() + 1);
                        ticket6.setCount(ticket6.getCount() + 1);
                    }
                    ticket4.setCount( ticket5.getCount() > ticket6.getCount()?ticket5.getCount():ticket6.getCount() );
                    ticketMap.put(4,ticket4);
                    ticketMap.put(5,ticket5);
                    ticketMap.put(6,ticket6);
                }
                ticketDao.saveAllTicket(ticketMap);
                return "退票成功";
            }
            else{
                return "退票失败";
            }
        }

    }

}
